package mapper;

import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import pojo.Order;
import pojo.User;

import java.util.List;

public interface OrderMapper {
    /**
     * 一对一多表查询
     * 通过订单查询订单信息和该订单的用户信息
     *
     * 方法1:一次性写完SQL语句
     * @return
     */
    @Select("select *,orders.id as oid from orders,user where orders.uid = user.id")
    @Results({
            @Result(column = "oid",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
            @Result(column = "uid",property = "user.id"),
            @Result(column = "username",property = "user.username"),
            @Result(column = "password",property = "user.password"),
            @Result(column = "birthday",property = "user.birthday"),
    })
    public List<Order> findAll();

    /**
     * 一对一多表查询
     * 通过订单查询订单信息和该订单的用户信息
     *
     * 方法2:使用One注解的查询
     * 此方式使用比较广泛
     * @return
     */
    @Select("select * from orders")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "ordertime",property = "ordertime"),
            @Result(column = "total",property = "total"),
            @Result(
                    property = "user",//要封装的属性名称
                    column = "uid",//根据哪个字段去查询user表的数据
                    javaType = User.class,//要封装的实体类型
                    //select属性:代表查询哪个接口的哪个方法去获得数据
                    one = @One(select = "mapper.UserMapper.findById")
            )
    })
    public List<Order> findAllByOne();

    /**
     * 通过用户id查询订单
     * @param uid userid
     * @return 该用户的全部订单
     */
    @Select("select * from orders where uid = #{id}")
    public List<Order> findByUid(int uid);
}
